package com.activity;

import java.nio.charset.Charset;

import com.wot2013.R;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.os.Parcelable;
import android.widget.TextView;
import android.widget.Toast;

public class WidgetConnectActivity extends Activity{
	
	TextView textView;
	NfcAdapter mNfcAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.widget_nfc);
		
		ActionBar actionBar = getActionBar();
		actionBar.hide();

	}
	
	@Override
	public void onResume(){
		super.onResume();
		// check if it starts because of NFC Beam
		if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())){
			processIntent(getIntent());
		}
	}
		
	@Override
	public void onNewIntent(Intent intent){
		//before execute onResume(), set intent
		setIntent(intent);
	}
		
	void processIntent(Intent intent){
		//process NDEF Message
		Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
				NfcAdapter.EXTRA_NDEF_MESSAGES);
		//only one message sent during the beam
		NdefMessage msg = (NdefMessage) rawMsgs[0];
		//record 0 contains the MIME type, record 1 is the AAR, if present 
		Toast.makeText(this, new String(msg.getRecords()[0].getPayload()),
				Toast.LENGTH_SHORT).show();
	}
	public NdefRecord createMimeRecord(String mimeType, byte[] payload) {
		//create a MIME type for NDEF record
		byte[] mimeBytes = mimeType.getBytes(Charset.forName("US-ASCII"));
		NdefRecord mimeRecord = new NdefRecord(
				NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload);
		return mimeRecord;
	}
	
	
}
